package com.xckj.cycling.framework

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import com.xckj.cycling.viewmodel.LoginViewModel
import org.greenrobot.eventbus.EventBus

abstract class BaseFragmentKt : Fragment() {

    protected val loginViewModel: LoginViewModel by lazyGlobalViewModel()
    private var vb: ViewBinding? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val local = vb?.apply {
            (root.parent as ViewGroup?)?.removeView(root)
        } ?: MvvmUtils.findViewBinding(this, container, layoutInflater)?.apply {
//            val v: View? = root?.findViewById(R.id.common_top_bar_view)
//            if (v != null) {
//                val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, QMUIStatusBarHelper.getStatusbarHeight(activity))
//                v.layoutParams = FrameLayout.LayoutParams(params)
//            }
            initView(root, savedInstanceState)
            vb = this
        }
        ?: return null
        local.apply {
            (root.parent as ViewGroup?)?.removeView(root)
            return root
        }
    }

    protected open fun initView(root: View, savedInstanceState: Bundle?) {
    }

    open fun getBaseActivity(): BaseActivityKt? {
        return activity as BaseActivityKt?
    }

    override fun onResume() {
        super.onResume()
        if (!isHidden) {
            resumeAndNoHidden()
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden && isResumed) {
            resumeAndNoHidden()
        }
    }

    open fun resumeAndNoHidden() {

    }


    fun showLoading() {
        if (activity is BaseActivityKt) {
            (activity as BaseActivityKt).showLoading()
        }
    }

    fun hideLoading() {
        if (activity is BaseActivityKt) {
            (activity as BaseActivityKt).hideDialog()
        }
    }

    fun showMessage(chars: CharSequence?) {
        if (activity is BaseActivityKt) {
            (activity as BaseActivityKt).showMessage(chars)
        }
    }

    fun safeRegEventBus() {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                if (!EventBus.getDefault().isRegistered(this@BaseFragmentKt)) {
                    EventBus.getDefault().register(this@BaseFragmentKt)
                }
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                if (EventBus.getDefault().isRegistered(this@BaseFragmentKt)) {
                    EventBus.getDefault().unregister(this@BaseFragmentKt)
                }
            }
        })
    }
}